package com.yisu.expressway.manager.website.controller.goodsArchives;

import com.yisu.expressway.ewf.common.web.EasyUIGrid;
import com.yisu.expressway.ewf.common.web.JsonResult;
import com.yisu.expressway.ewf.entity.GoodsArchives;
import com.yisu.expressway.ewf.entity.GoodsCategory;
import com.yisu.expressway.manager.core.service.IEditorActionService;
import com.yisu.expressway.manager.core.service.IGoodsArchivesService;
import com.yisu.expressway.manager.core.service.IGoodsCategory;
import com.yisu.expressway.manager.domain.request.GoodsArchivesRequest;
import com.yisu.expressway.manager.domain.request.GoodsSpecificationRequest;
import com.yisu.expressway.manager.domain.vo.RecommendGoodsForSelVo;
import com.yisu.expressway.manager.website.controller.BaseController;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.yisu.expressway.ewf.common.enums.ResponseCode.FAILURE;
import static com.yisu.expressway.ewf.common.enums.ResponseCode.SUCCESS;

/**
 * Created by xlt on 2017/4/24.
 */
@Controller
@RequestMapping(value = "/categoryGoods")
public class CategoryGoodsController extends BaseController {
    @Resource
    private IGoodsArchivesService iGoodsArchivesService;
    @Resource
    private IGoodsCategory iGoodsCategory;

    @RequestMapping(value = "/index.htm")
    public String listPage(Model model) {
        logger.info("Go to goodsArchives categoryGoods index page...");
        try {
            model.addAttribute("titleName", "商品档案管理");
        } catch (Exception ex) {
            logger.error("商品档案管理加载异常：" + ex.getMessage(), ex);
        }
        return "/goodsArchives/categoryGoods/index";
    }

    @RequestMapping(value = "/goodsList.htm")
    public String goodsList(Model model, GoodsArchivesRequest goodsArchivesRequest) {
        logger.info("Go to goodsArchives categoryGoods index page...");
        try {
            if (goodsArchivesRequest.getKeyword() != null) {
                goodsArchivesRequest.setKeyword(URLDecoder.decode(goodsArchivesRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<GoodsArchives> goodsArchivesEasyUIGrid = iGoodsArchivesService.getGoodsArchivesByCategoryCodeByPage(goodsArchivesRequest);
            String pathNodeName = "";
            if (!StringUtils.isEmpty(goodsArchivesRequest.getCategoryCode())) {
                pathNodeName = iGoodsCategory.getGoodsCategoryPathNameByGoodsCategoryCode(goodsArchivesRequest.getCategoryCode());
            }
            model.addAttribute("GoodsArchivesVos", goodsArchivesEasyUIGrid.getRows());
            model.addAttribute("currentPage", goodsArchivesRequest.getPage());
            model.addAttribute("totalPages", (goodsArchivesRequest.getTotalRecords() / goodsArchivesRequest.getRows()) + 1);
            model.addAttribute("titleName", "商品档案管理");
            model.addAttribute("pathNodeName", pathNodeName);
            model.addAttribute("keyword", goodsArchivesRequest.getKeyword());
            model.addAttribute("categoryCode", goodsArchivesRequest.getCategoryCode());
        } catch (Exception ex) {
            logger.error("商品档案管理加载异常：" + ex.getMessage(), ex);
        }
        return "/goodsArchives/categoryGoods/goodsList";
    }


    @RequestMapping(value = "/goodsDetail.htm")
    public String goodsDetail(Model model, GoodsArchivesRequest goodsArchivesRequest) {
        logger.info("Go to goodsArchives categoryGoods goodsDetail page...");
        try {
            GoodsArchives goodsArchives = new GoodsArchives();
            SimpleDateFormat sformat = new SimpleDateFormat("yyyyMMddHHmmss");
            Long fakeGoodsNo = Long.valueOf(sformat.format(new Date()));
            if (StringUtils.isEmpty(goodsArchivesRequest.getGoodsNo() + "")) {
                goodsArchives.setGoodsNo(fakeGoodsNo);
                goodsArchives.setCategoryCode(goodsArchivesRequest.getCategoryCode());
            } else {
                goodsArchives = iGoodsArchivesService.getGoodsArchivesByGoodsNo(goodsArchivesRequest.getGoodsNo());
                if (goodsArchives == null) {
                    goodsArchives = new GoodsArchives();
                    goodsArchives.setGoodsNo(fakeGoodsNo);
                    goodsArchives.setCategoryCode(goodsArchivesRequest.getCategoryCode());
                }
            }
            String pathNodeName = "";
            if (!StringUtils.isEmpty(goodsArchivesRequest.getCategoryCode())) {
                pathNodeName = iGoodsCategory.getGoodsCategoryPathNameByGoodsCategoryCode(goodsArchivesRequest.getCategoryCode());
            }
            model.addAttribute("titleName", "商品档案管理");
            model.addAttribute("pathNodeName", pathNodeName);
            model.addAttribute("goodsArchivesVo", goodsArchives);
        } catch (Exception ex) {
            logger.error("商品档案管理加载异常：" + ex.getMessage(), ex);
        }
        return "/goodsArchives/categoryGoods/goodsDetail";
    }


    /**
     * 编辑商品档案信息
     *
     * @param goodsArchives 商品档案信息
     * @return 状态
     */
    @RequestMapping(value = "/modifyCategoryGoodsAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult modifyCategoryGoodsAction(HttpServletRequest request, GoodsArchives goodsArchives) {
        try {
            if (iGoodsArchivesService.modifyGoodsArchives(goodsArchives)) {
                return new JsonResult(goodsArchives);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(FAILURE.code, FAILURE.msg);
    }

    /**
     * 更新类目下的商品排序值
     *
     * @param goodsArchivesRequest 商品档案信息
     * @return 状态
     */
    @RequestMapping(value = "/updateCategoryGoodsSortAction.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult updateCategoryGoodsSortAction(HttpServletRequest request, GoodsArchivesRequest goodsArchivesRequest) {
        try {
            try {
                Integer.parseInt(goodsArchivesRequest.getDomSort());
            } catch (Exception ex) {
                ex.printStackTrace();
                return new JsonResult(FAILURE.code, "请输入合法的商品排序值");
            }
            if (iGoodsArchivesService.updateCategoryGoodsSort(goodsArchivesRequest)) {
                return new JsonResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(FAILURE.code, "网络君泡妞去了.....");
    }

    /**
     * 删除商品档案信息
     *
     * @param goodsArchivesRequest 商品档案信息
     * @return 状态
     */
    @RequestMapping(value = "/deleteGoodsArchive.json", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JsonResult deleteGoodsArchive(HttpServletRequest request, GoodsArchivesRequest goodsArchivesRequest) {
        try {
            if (goodsArchivesRequest.getGoodsNo() == null || goodsArchivesRequest.getGoodsNo() == 0) {
                return new JsonResult(FAILURE.code, "非法请求！");
            }
            goodsArchivesRequest.setStatus(-1);
            if (iGoodsArchivesService.updateStatus(goodsArchivesRequest)) {
                return new JsonResult(SUCCESS.code, "操作成功！");
            } else {
                return new JsonResult(FAILURE.code, "网络君泡妞去了！");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new JsonResult(FAILURE.code, "网络君泡妞去了.....");

    }


    @RequestMapping(value = "/selCategoryGoodsList.htm")
    public String selCategoryGoodsList(Model model, GoodsArchivesRequest goodsArchivesRequest) {
        logger.info("Go to goodsArchives categoryGoods index page...");
        try {
            if (goodsArchivesRequest.getKeyword() != null) {
                goodsArchivesRequest.setKeyword(URLDecoder.decode(goodsArchivesRequest.getKeyword(), "utf-8"));
            }
            EasyUIGrid<RecommendGoodsForSelVo> goodsArchivesEasyUIGrid = iGoodsArchivesService.getGoodsArchivesByParentCategoryCodeForAllByPage(goodsArchivesRequest);
            model.addAttribute("GoodsArchivesVos", goodsArchivesEasyUIGrid.getRows());
            model.addAttribute("currentPage", goodsArchivesRequest.getPage());
            model.addAttribute("totalPages", (goodsArchivesRequest.getTotalRecords() / goodsArchivesRequest.getRows()) + 1);
            model.addAttribute("titleName", "商品档案管理");
            model.addAttribute("keyword", goodsArchivesRequest.getKeyword());
            model.addAttribute("categoryCode", goodsArchivesRequest.getCategoryCode());
        } catch (Exception ex) {
            logger.error("商品档案管理加载异常：" + ex.getMessage(), ex);
        }
        return "/goodsArchives/categoryGoods/selCategoryGoodsList";
    }

}
